import {defineStore} from 'pinia'
import {
  getReprotPrint,
  getBarcodePrint,
  getPdfPrint,
  saveReprotPrint,
  saveBarcodePrint,
  savePdfPrint
} from '@/util/dkmk'

//状态
export enum CLodopStatus{
  NotReady, Loading, Ready, NeedInstall, NeedUpgrade
}

export interface CLodopStateType {
  cLodopSataus: CLodopStatus //打印服务状态
  cLodopPorts: Array<number> //打印服务端口 一般默认两个
  reportPrint: string | null //报告打印机
  barcodePrint: string | null //条码打印机
  pdfPrint: string | null //PDF打印机
  showPrinter: boolean //显示打印机选择
}

const cLodopStore = defineStore('cLodopStore', {
  state: (): CLodopStateType => {
    return {
      cLodopSataus: CLodopStatus.NotReady,
      cLodopPorts: [8000, 18000],
      reportPrint: getReprotPrint(),
      barcodePrint: getBarcodePrint(),
      pdfPrint: getPdfPrint(),
      showPrinter: false
    }
  },

  getters: {
    getLodopStatus: (state) => {
      return state.cLodopSataus
    },
    getLodopPorts: (state) => {
      return state.cLodopPorts
    },
    //是否设置了条码和报告打印机
    getPrintStatus: (state) => {
      return state.barcodePrint && state.reportPrint && state.pdfPrint
        ? true
        : false
    },
    //获取条码打印机
    getBarcodePrint: (state) => {
      return state.barcodePrint
    },
    //获取报告打印机
    getReprotPrint: (state) => {
      return state.reportPrint
    },
    //获取PDF打印机
    getPdfPrint: (state) => {
      return state.pdfPrint
    },
    //是否显示打印机
    getShowPrinter: (state) => {
      return state.showPrinter
    }
  },
  actions: {
    setLodopStatus(data: CLodopStatus) {
      this.cLodopSataus = data
    },
    setLodopPorts(ports: Array<number>) {
      //需要重新加载
      this.cLodopPorts = ports
    },
    //设置条码打印机
    setLodopBarcode(barcodePrint: string) {
      this.barcodePrint = barcodePrint
      saveBarcodePrint(barcodePrint)
    },
    //设置报告打印机
    setLodopReport(reportPrint: string) {
      this.reportPrint = reportPrint
      saveReprotPrint(reportPrint)
    },
    //设置PDF打印机
    setPdfReport(reportPrint: string) {
      this.pdfPrint = reportPrint
      savePdfPrint(reportPrint)
    },
    //设置是否显示打印机选择
    setShowPrinter(show: boolean) {
      this.showPrinter = show
    }
  }
})

export default {
  setup() {
    const store = cLodopStore()
    return {store}
  }
}
